Skip to content

修复 1.2.5 structuredClone错误#1192

Merged
CodFrm merged 21 commits intoscriptscat:mainfrom
cyfung1031:pr-fix-clone-01
Feb 3, 2026
Merged

修复 1.2.5 structuredClone错误#1192
CodFrm merged 21 commits intoscriptscat:mainfrom
cyfung1031:pr-fix-clone-01

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Feb 3, 2026

概述 Descriptions

注:日后把一些共通页面环境的东西都拉去 global.ts (1.3.x) [ 例如 performance pageDispatch, ... ]
避免页面内容修改过而无法运行内部处理

变更内容 Changes

截图 Screenshots

@CodFrm
Copy link
Member

CodFrm commented Feb 3, 2026

一个兼容TM的示例,很极端的用法,说实话不是很想咬文嚼字,内部实现本来就不一样,硬要一致会多一些莫名其妙的代码,不用完全一致

// ==UserScript==
// @name         New Userscript REDV-1
// @namespace    https://docs.scriptcat.org/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=example.com
// @early-start
// @run-at       document-start
// @grant     GM_setValue
// @grant GM_getValue
// @grant GM_setValues
// @grant GM_getValues
// ==/UserScript==

console.log(GM_getValue("circular"), GM_getValues(["circular"])); // undefined {}
GM_setValues({ "circular": "123" });
console.log(GM_getValue("circular"), GM_getValues(["circular"])); // 123 {"circular":123}
GM_setValues({ "circular": window });
console.log(GM_getValue("circular"), GM_getValues(["circular"])); // undefined {circular: undefined}
GM_setValue("circular", window)
console.log(GM_getValue("circular"), GM_getValues(["circular"])); // undefined {circular: undefined}

@cyfung1031
Copy link
Collaborator Author

不用完全一致

你的 undefined 会删键呀
TM 的 undefined 不会

a.scriptRes.value[key] = value;
a.sendMessage("GM_setValue", [id, key, value]);
if (value === undefined) {
a.sendMessage("GM_setValue", [id, key]);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的 a.sendMessage("GM_setValue", [id, key]); 会有 delete a.scriptRes.value[key]; 但这个是 a.scriptRes.value[key] = undefined;

你要不要统一一下呀?

Copy link
Member

@CodFrm CodFrm Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

故意为之,兼容TM,删除了key,但是这一次通过 GM_getValues 可以获取到 key,但是刷新页面就没有这个key了

value_ = customClone(value_);
}
// customClone 可能返回 undefined
valueStore[key] = value_;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面有 delete valueStore[key];
这里是 valueStore[key] = undefined;

你要不要统一一下呀?

Comment on lines 273 to 288
if (value === undefined) {
delete a.scriptRes.value[key];
a.sendMessage("GM_setValue", [id, key]);
} else {
// 对object的value进行一次转化
if (value && typeof value === "object") {
value = structuredClone(value);
value = customClone(value);
}
// customClone 可能返回 undefined
a.scriptRes.value[key] = value;
a.sendMessage("GM_setValue", [id, key, value]);
if (value === undefined) {
a.sendMessage("GM_setValue", [id, key]);
} else {
a.sendMessage("GM_setValue", [id, key, value]);
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
// 对object的value进行一次转化
if (value && typeof value === "object") {
value = customClone(value);
// customClone 可能返回 undefined
}
if (value === undefined) {
delete a.scriptRes.value[key];
a.sendMessage("GM_setValue", [id, key]);
} else {
a.scriptRes.value[key] = value;
a.sendMessage("GM_setValue", [id, key, value]);
}

这样简单一点不好吗

Comment on lines 304 to 317
for (const [key, value] of Object.entries(values)) {
let value_ = value;
if (value_ === undefined) {
if (valueStore[key]) delete valueStore[key];
} else {
// 对object的value进行一次转化
if (value_ && typeof value_ === "object") {
value_ = structuredClone(value_);
value_ = customClone(value_);
}
// customClone 可能返回 undefined
valueStore[key] = value_;
values[key] = value_;
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
for (const [key, value] of Object.entries(values)) {
let value_ = value;
// 对object的value进行一次转化
if (value_ && typeof value_ === "object") {
value_ = customClone(value_);
// customClone 可能返回 undefined
}
if (value_ === undefined) {
if (valueStore[key]) delete valueStore[key];
} else {
valueStore[key] = value_;
}
}

这样简单一点不好吗

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

故意为之,兼容TM

@CodFrm
Copy link
Member

CodFrm commented Feb 3, 2026

单测出问题了

@CodFrm
Copy link
Member

CodFrm commented Feb 3, 2026

不用完全一致

你的 undefined 会删键呀 TM 的 undefined 不会

TM会删啊,你看刷新后,第二次进来就知道了

@CodFrm CodFrm merged commit 265e122 into scriptscat:main Feb 3, 2026
1 check passed
CodFrm added a commit that referenced this pull request Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 更新到1.2.5版本后 Bilibili-Evolved 无法正常加载

2 participants